﻿using System;
using System.Collections.Generic;
using System.Text;
using DO.Clustering;

namespace BLL.Relations.VectorDistances
{
    /// <summary>
    /// 
    /// </summary>
    public class FeatureWeightCalculator
    {
        /// <summary>
        /// all shared Feature are treated equally, skipping any non-shared Features
        /// </summary>
        /// <param name="FeatureCountInDoc1"></param>
        /// <param name="FeatureCountInDoc2"></param>
        /// <param name="sharedFeatureCount"></param>
        /// <returns></returns>
        public static double TaminoCommonFeatureScale(int FeatureCountInDoc1, int FeatureCountInDoc2, int sharedFeatureCount)
        {
            double contributingFeatureScale = Math.Log10(FeatureCountInDoc1 + FeatureCountInDoc2);
            if (sharedFeatureCount > 0)
            {
                contributingFeatureScale =
                    Math.Log(
                        (double) (FeatureCountInDoc1 + FeatureCountInDoc2 - sharedFeatureCount)/
                        sharedFeatureCount, 2);
            }
            return contributingFeatureScale;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="FeatureFreqs1"></param>
        /// <param name="FeatureFreqs2"></param>
        /// <param name="FeatureWeights"></param>
        /// <returns></returns>
        public static double TaminoWeightedFeatureScale(
            Dictionary<int,FeatureFreq> FeatureFreqs1,
            Dictionary<int,FeatureFreq> FeatureFreqs2,
            Dictionary<int,double> FeatureWeights)
        {
            List<int> FeatureIDs=new List<int>();
            foreach(int FeatureID in FeatureFreqs1.Keys)
            {
                FeatureIDs.Add(FeatureID);
            }
            foreach(int FeatureID in FeatureFreqs2.Keys)
            {
                if(!FeatureIDs.Contains(FeatureID))
                    FeatureIDs.Add(FeatureID);
            }
            double weightedSum = 0;
            double sharedWeight = 0;
            foreach(int FeatureID in FeatureIDs)
            {
                if (FeatureWeights.ContainsKey(FeatureID))
                    weightedSum += FeatureWeights[FeatureID];
                if (FeatureFreqs1.ContainsKey(FeatureID) && FeatureFreqs2.ContainsKey(FeatureID) && FeatureWeights.ContainsKey(FeatureID))
                    sharedWeight += FeatureWeights[FeatureID];
            }
            double contributingFeatureScale = 1;
            if(sharedWeight>0)
                contributingFeatureScale = Math.Log(weightedSum / sharedWeight, 2);
            
            return contributingFeatureScale;
        }
    }
}
